package cn.wolfcode.controller;


import cn.wolfcode.entity.Destination;
import cn.wolfcode.entity.Strategy;
import cn.wolfcode.entity.Travel;
import cn.wolfcode.entity.Userinfo;
import cn.wolfcode.es.domain.DestinationES;
import cn.wolfcode.es.domain.StrategyES;
import cn.wolfcode.es.domain.TravelES;
import cn.wolfcode.es.domain.UserInfoES;
import cn.wolfcode.es.service.IDestinationEsService;
import cn.wolfcode.es.service.IStrategyEsService;
import cn.wolfcode.es.service.ITravelEsService;
import cn.wolfcode.es.service.IUserInfoEsService;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserinfoService;
import cn.wolfcode.util.JSONResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserinfoService userinfoService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyEsService strategyEsService;

    @Autowired
    private ITravelEsService travelEsService;

    @Autowired
    private IUserInfoEsService userInfoEsService;

    @Autowired
    private IDestinationEsService destinationEsService;

    @GetMapping("/dataInit")
    public JSONResult dataInit() {
        // 攻略
        List<Strategy> strategies = strategyService.list();
        for (Strategy strategy : strategies) {
            StrategyES strategyES = new StrategyES();
            BeanUtils.copyProperties(strategy, strategyES);
            strategyEsService.save(strategyES);
        }

        // 游记
        List<Travel> travels = travelService.list();
        for (Travel travel : travels) {
            TravelES travelES = new TravelES();
            BeanUtils.copyProperties(travel, travelES);
            travelEsService.save(travelES);
        }

        // 目的地
        List<Destination> destinations = destinationService.list();
        for (Destination destination : destinations) {
            DestinationES destinationES = new DestinationES();
            BeanUtils.copyProperties(destination, destinationES);
            destinationEsService.save(destinationES);
        }
        // 用户信息
        List<Userinfo> userinfos = userinfoService.list();
        for (Userinfo userinfo : userinfos) {
            UserInfoES userInfoES = new UserInfoES();
            BeanUtils.copyProperties(userinfo, userInfoES);
            userInfoEsService.save(userInfoES);
        }


        return JSONResult.success();
    }
}
